package hn.sigit.view;

import hn.sigit.dao.hnd.cadastre.HND_LandUseDAO;
import hn.sigit.model.hnd.administrative.HND_SpatialRule;
import hn.sigit.model.hnd.cadastre.HND_LandUse;

import java.io.Serializable;
import java.util.List;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Name("selectLandUseHelper")
@Scope(value = ScopeType.CONVERSATION)
@AutoCreate
public class SelectLandUseHelper implements Serializable {
	private static final long serialVersionUID = 1L;

	private int currentLevel = 0;
	private List<HND_LandUse> currentLandUseList;
	private HND_LandUse selected;
	
	private String landUseIdInputFieldName = "";
	private String landUseNameInputFieldName = "";
	private String landUseField;

	
	public int getCurrentLevel() {
		return currentLevel;
	}

	public List<HND_LandUse> getCurrentLandUseList() {
		if (currentLandUseList == null) {
			if (currentLevel == 0)
				currentLandUseList = HND_LandUseDAO.loadLandUsesByLevel(0, true);
			else if (selected != null)
				currentLandUseList = HND_LandUseDAO.loadLandUsesByParent(selected, true);
		}
		return currentLandUseList;
	}
	public void setCurrentLandUseList(List<HND_LandUse> currentLandUseList) {
		this.currentLandUseList = currentLandUseList;
	}

	public HND_LandUse getSelected() {
		return selected;
	}
	public void setSelected(HND_LandUse selected) {
		this.selected = selected;
	}

	public String getLandUseIdInputFieldName() {
		return landUseIdInputFieldName;
	}
	public void setLandUseIdInputFieldName(String landUseIdInputFieldName) {
		this.landUseIdInputFieldName = landUseIdInputFieldName;
	}

	public String getLandUseNameInputFieldName() {
		return landUseNameInputFieldName;
	}
	public void setLandUseNameInputFieldName(String landUseNameInputFieldName) {
		this.landUseNameInputFieldName = landUseNameInputFieldName;
	}
	
	public String getLandUseField() {
		return landUseField;
	}
	public void setLandUseField(String landUseField) {
		this.landUseField = landUseField;
	}

	public boolean isInputFieldSpecified() {
		return !landUseIdInputFieldName.equals("") && !landUseNameInputFieldName.equals("");
	}
	
	public void setInputFields(String idField, String nameField) {
		landUseIdInputFieldName = idField;
		landUseNameInputFieldName = nameField;
	}
	
	public String getSelectedIdStr() {
		if (selected == null) return "";
		
		return selected.getId().toString().replaceAll("'", "\\'");
	}
	
	public String getSelectedNameStr() {
		if (selected == null) return "";
		
		return String.format("%s - %s", selected.getCompleteCode(), selected.getName()).replaceAll("'", "\\'");
	}

	public String landUseName(HND_LandUse lu) {
		if (lu == null) return "";
		
		return String.format("%s - %s", lu.getCompleteCode(), lu.getName()).replaceAll("'", "\\'");
	}

	public void drillDown(HND_LandUse landUse) {
		selected = landUse;
		currentLevel++;
		currentLandUseList = null;
	}
	
	public void rollUp() {
		if (currentLevel == 0) return;
		
		if (selected.getParent() != null)
			selected = selected.getParent();
		currentLevel--;
		currentLandUseList = null;
	}
	
	public void select(HND_LandUse landUse) {
		this.selected = landUse;
		takeSelection();
	}

	public String takeSelection() {
		if (landUseField != null) {
			Application app = FacesContext.getCurrentInstance().getApplication();
			ExpressionFactory exprFactory = app.getExpressionFactory();
			ELContext elCtx = FacesContext.getCurrentInstance().getELContext();
			ValueExpression ve;
			
			ve = exprFactory.createValueExpression(elCtx, "#{" + landUseField + "}", HND_LandUse.class);
			ve.setValue(elCtx, selected);
		}
		return null;
	}
}
